(:#############################################################################################

    Parse alle Komponisten Listen (*.xhtml) und schreibe sie in eine gemeinsame Datei.
    Splitte zudem Nachname, Vorname, Geburtsjahr und Sterbejahr auf
    
    Warum XQuery: Das Parsen mehrerer Datein funktioniert mit XQuery einfacher.

#############################################################################################:)

declare default element namespace "http://www.w3.org/1999/xhtml";

declare function local:extractForename($composer as xs:string?)
as element(Forename)?
{
	let $composer := replace($composer, '\n', '')
	return
	if (matches($composer, '^.*?,.*?$')) then (
	   if (matches($composer, '^.*?, (.*?)\([^(]*[0-9]+.*?\)$')) then (
	       let $vorname := replace($composer, '^.*?, (.*?)\([^(]*[0-9]+.*?\)$', '$1')
	       return <Forename> { normalize-space($vorname) } </Forename>
	   ) else (
	       let $vorname := replace($composer, '^.*?, (.*?)$', '$1')
	       return <Forename> { normalize-space($vorname) } </Forename>
	   )
	) else ()
};

declare function local:extractSurname($composer as xs:string?)
as element(Surname)?
{
	let $composer := replace($composer, '\n', '')
	return
	if (matches($composer, '^.*?,.*?$')) then (
       let $nachname := replace($composer, '^(.*?), .*?$', '$1')
       return <Surname> { normalize-space($nachname) } </Surname>
	) else (
	   if (matches($composer, '^(.*?)\([^(]*[0-9]+.*?\)$')) then (
	       let $nachname := replace($composer, '^(.*?)\([^(]*[0-9]+.*?\)$', '$1')
	       return <Surname> { normalize-space($nachname) } </Surname>
	   ) else (
	       let $nachname := $composer
	       return <Surname> { normalize-space($nachname) } </Surname>
	   )
	)
};

declare function local:extractYearOfBirth($composer as xs:string?)
as element(YearOfBirth)?
{
	let $composer := replace($composer, '\n', '')
	let $jahr := replace($composer, "^.*?\(([^(]*?[0-9]{4}.*?)\).*?$", "$1")
	return
	if (matches($composer, '^.*?[0-9]+.*?$') and not(matches($composer, 'Jahrhundert'))) then (
       	if (matches($composer, 'geb')) then (
       		let $jahr := replace($jahr, "geb.*?([0-9]{4})", "$1")
       		return <YearOfBirth> { $jahr } </YearOfBirth>
       	) else (
       		let $jahr := replace($jahr, "([0-9]{4})-[0-9]{4}", "$1")
       		return <YearOfBirth> { $jahr } </YearOfBirth>
       	)
    ) else ()
};

declare function local:extractYearOfDeath($composer as xs:string?)
as element(YearOfDeath)?
{
	let $composer := replace($composer, '\n', '')
	let $jahr := replace($composer, "^.*\((.*?[0-9]{4}.*?)\).*$", "$1")
	return
	if (matches($composer, '^.*?[0-9]+.*?$') and not(matches($composer, 'geb')) and not(matches($composer, 'Jahrhundert'))) then (
       	let $jahr := replace($jahr, "[0-9]{4}-([0-9]{4})", "$1")
       	return <YearOfDeath> { $jahr } </YearOfDeath>
   ) else ()
};

declare function local:extractCentury($composer as xs:string?)
as element(Century)?
{
	let $composer := replace($composer, '\n', '')
	let $jahr := replace($composer, "^.*\((.*?[0-9]{4}.*?)\).*$", "$1")
	return
	if (matches($composer, 'Jahrhundert')) then (
		let $jahr := replace($jahr, ".*?\(([0-9]{2}). *?Jahrhundert.*?\).*?", "$1")
		return  <Century> { $jahr } </Century>
	) else ()
};



<Composers> {
for $list in collection("Composers?select=*.xhtml")
    order by $list
    return for $composer in $list//div[@id = 'personen_s1v3' or @id= 'personen_s2v3']/a
        return <Composer>
                   { local:extractSurname($composer) }
                   { local:extractForename($composer) }
                   { local:extractYearOfBirth($composer) }
                   { local:extractYearOfDeath($composer) }
                   { local:extractCentury($composer) }
                   <InfoURI> { data($composer/@href) } </InfoURI>
               </Composer>
} </Composers> 
